#include "sences.h"

extern long start;
long start1,start2;
extern SPRITE maybay;
extern LPDIRECT3DSURFACE9 back;
extern LPDIRECT3DDEVICE9 pDevice;

//sprite handler
extern LPD3DXSPRITE sprite_handler;
extern D3DXVECTOR3 position;
extern LPDIRECT3DTEXTURE9 plane_fight[3],truc_thang[9];
extern int menuItemSelected;

extern LPDIRECT3DTEXTURE9 gNewGame,gIntroduction,gQuit,gOption,gHighScore,gbanner,p_dan,p_ten_lua;
extern LPDIRECT3DTEXTURE9 gLNewGame,gLIntroduction,gLQuit,gLOption,gLHighScore;
extern LPDIRECT3DTEXTURE9 status_ten_lua,status_menu,status_heart,status_khien,boss_cuoi;


extern CSound *sound_bounce;
extern CSound *sound_hit;
extern CSound *nhac_nen;
extern CSound *menu_bip,*s_m16,*s_ten_lua;


SPRITE dan[20];
SPRITE ten_lua[20];
SPRITE truc_thang1;

int Number_tenlua = 40; 

void gMenuGame() {
	 D3DXVECTOR3 position(0,0,0);
	  position.x = (float)0;
      position.y = (float)0;
      sprite_handler->Draw(
            plane_fight[0], 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));
		// banner
	  position.x = (float)79.0;
      position.y = (float)92.0;
      sprite_handler->Draw(
            gbanner, 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));
		// New Game
	  position.x = (float)280.0;
      position.y = (float)303.75;
      sprite_handler->Draw(
            gNewGame, 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));
	  // Introduction
	  position.x = (float)265.0;
      position.y = (float)366.0;
      sprite_handler->Draw(
            gIntroduction, 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));
	  //High Score
	  position.x = (float)282.0;
      position.y = (float)418.0;
      sprite_handler->Draw(
            gHighScore, 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));
	  // Option
	  position.x = (float)323.0;
      position.y = (float)472.0;
      sprite_handler->Draw(
            gOption, 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));
	  //Quit
	  position.x = (float)352.0;
      position.y = (float)522.0;
      sprite_handler->Draw(
            gQuit, 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));

}
void Menu_Update(int items){

	 switch (items) {
	 case  0: // New Game
			  position.x = (float)280.0;
			  position.y = (float)303.75;
			  sprite_handler->Draw(
					gLNewGame, 
					NULL,
					NULL,
					&position,
					D3DCOLOR_XRGB(255,255,255));
			  break;
	 
	 case 1:
			  position.x = (float)265.0;
			  position.y = (float)366.0;
			  sprite_handler->Draw(
					gLIntroduction, 
					NULL,
					NULL,
					&position,
					D3DCOLOR_XRGB(255,255,255));

			  break;
	 case 2:
 position.x = (float)282.0;
      position.y = (float)418.0;
      sprite_handler->Draw(
            gLHighScore, 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));

			  break;
	 case 3:
	  position.x = (float)323.0;
      position.y = (float)472.0;
      sprite_handler->Draw(
            gLOption, 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));

			  break;
	 case 4:
	  position.x = (float)352.0;
      position.y = (float)522.0;
      sprite_handler->Draw(
            gLQuit, 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));

			  break;


	 }

}

RECT rect;
D3DXFONT_DESC fontDesc; 
ID3DXFont* Font = 0; 
	char abc[10];
bool tao = false;
void khoitao()
{
	rect.left = 425;
	rect.right = 500;
	rect.top =4;
	rect.bottom = 100;

	truc_thang1.x = 100;
	truc_thang1.y = 100;
	truc_thang1.curframe = 1;
	truc_thang1.lastframe = 8;

	//  itoa (i,abc,10);
	

ZeroMemory(&fontDesc, sizeof(D3DXFONT_DESC)); 
fontDesc.Height = 30; 
fontDesc.Width = 18; 
fontDesc.Weight = 500; 
fontDesc.MipLevels = D3DX_DEFAULT; 
fontDesc.Italic = false; 
fontDesc.CharSet = 0; 
fontDesc.OutputPrecision = 0; 
fontDesc.Quality = 0; 
fontDesc.PitchAndFamily = 0; 
strcpy(fontDesc.FaceName, "Times New Roman"); // font style

D3DXCreateFontIndirect(d3ddev, &fontDesc, &Font);
tao = true;
}


void ve(int i)

{
	itoa (i,abc,10);

Font->DrawText( 
NULL, 
abc, // String to draw. 
-1, // Null terminating string.
&rect, // Rectangle to draw the string in. 
DT_TOP | DT_LEFT, // Draw in top-left corner of rect.
0xFF0000FF); // Black.
}

void Sences::menu(void)
{
   if (GetTickCount() - start >= 50)
    {
        //reset timing
        start = GetTickCount();
	if (Key_Down(DIK_UP))
	{
		menuItemSelected = menuItemSelected -1;
		PlaySound(menu_bip);
		if (menuItemSelected<0)
			menuItemSelected = 0;

	}
		
	if (Key_Down(DIK_DOWN))
	{
		menuItemSelected = menuItemSelected +1;
		PlaySound(menu_bip);
		if (menuItemSelected>4)
			menuItemSelected = 4;
	}
	if (Key_Down(DIK_SPACE))
	{
		if (menuItemSelected == 0) 
			this->man = 1;
   }
   }
	 //   //start rendering
    if (d3ddev->BeginScene())
    {
        //erase the entire background
        d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);

        //start sprite handler
        sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);

 //       //draw the ball


		gMenuGame();
		Menu_Update(menuItemSelected);
	//	ve(menuItemSelected);
 //       //stop drawing
        sprite_handler->End();
 //      
 //       //stop rendering
        d3ddev->EndScene();
    }

}

int demi;
int temp;
bool init_dan = false;
int timDanDu(){
	for (demi=0 ;demi<20; demi++)
		if (dan[demi].alive == false) return demi;
	return 0;
}

int timTenLuaDu(){
	for (demi=0 ;demi<20; demi++)
		if (ten_lua[demi].alive == false) return demi;
	return 0;
}

void status_bar()
{
		
	//bar 
	position.x = (float)0.0;
     position.y = (float)0.0;
	 sprite_handler->Draw(status_menu, NULL,NULL,&position,D3DCOLOR_XRGB(255,255,255));
	// heart 
	 position.x = (float)586.0;
     position.y = (float)10.0;
     sprite_handler->Draw(status_heart, NULL,NULL,&position,D3DCOLOR_XRGB(255,255,255));
	// shield
	 position.x = (float)501.0;
     position.y = (float)6.0;
     sprite_handler->Draw(status_khien, NULL,NULL,&position,D3DCOLOR_XRGB(255,255,255));
	// ten lua
	 position.x = (float)405.0;
     position.y = (float)0.0;
     sprite_handler->Draw(status_ten_lua, NULL,NULL,&position,D3DCOLOR_XRGB(255,255,255));
	ve(Number_tenlua);
	position.x = (float)-100.0;
     position.y = (float)100.0;
     sprite_handler->Draw(boss_cuoi, NULL,NULL,&position,D3DCOLOR_XRGB(255,255,255));
}

void Sences::man1()
{
	if (tao == false) khoitao(); // khoi tao cai ve;
	if (init_dan == false) 
	{
		for (demi = 0; demi <20 ;demi++)
			dan[demi].alive = false;
		for (demi = 0; demi<20; demi++)
			ten_lua[demi].alive = false;
		init_dan = true;
	}

	if (GetTickCount() - start2 >= 100)
    {
		start2 = GetTickCount();
			if (truc_thang1.curframe++>=truc_thang1.lastframe)
			truc_thang1.curframe = 1;
	}
	if (GetTickCount() - start >= 30)
    {
        //reset timing
        start = GetTickCount();
	
	if (Key_Down(DIK_UP))
	{	
		maybay.y -= 10;
		maybay.curframe = 0;
	}
		
	if (Key_Down(DIK_DOWN))
	{	
		maybay.y += 10;
		maybay.curframe = 2;
	}
	if (Key_Down(DIK_LEFT))
	{	
		maybay.x -= 10;
	}
		
	if (Key_Down(DIK_RIGHT))
	{	
		maybay.x += 10;
	}


	if (Key_Down(DIK_SPACE))
	{
		temp = timDanDu();
		dan[temp].x = maybay.x-10;
		dan[temp].y = maybay.y+20;
		dan[temp].alive = true;
		StopSound(s_m16);
		PlaySound(s_m16);
	}
		if (Key_Down(DIK_F))
	{
		if (Number_tenlua>0) {
		temp = timTenLuaDu();
		ten_lua[temp].x = maybay.x-10;
		ten_lua[temp].y = maybay.y+20;
		ten_lua[temp].alive = true;

		Number_tenlua--;
		PlaySound(s_ten_lua);
		}
	}


   }
	if (GetTickCount() - start1 >= 20)
    {
        //reset timing
        start1 = GetTickCount();
		for (int i = 0; i<20; i++)
	{
		if (dan[i].alive == true)
		dan[i].x = dan[i].x -10;
		if (dan[i].x <0)
			dan[i].alive = false;

		if (ten_lua[i].alive == true)
		ten_lua[i].x = ten_lua[i].x -20;
		if (ten_lua[i].x <0)
			ten_lua[i].alive = false;


	}
	}
	 //   //start rendering
    if (d3ddev->BeginScene())
    {
        //erase the entire background
        d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);

        //start sprite handler
        sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);

 //       //draw the ball
		
		status_bar();
		 position.x = (float)maybay.x;
        position.y = (float)maybay.y;
        sprite_handler->Draw(
			plane_fight[maybay.curframe], 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));
		 position.x = (float)100;
        position.y = (float)100;
        sprite_handler->Draw(
			truc_thang[truc_thang1.curframe], 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));
		for (int i = 0; i<20; i++)
		{
			if (dan[i].alive == true)
			{
				position.x = (float)dan[i].x;
				position.y = (float)dan[i].y;
				        sprite_handler->Draw(
			p_dan, 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));

			}
			if (ten_lua[i].alive == true)
			{
				position.x = (float)ten_lua[i].x;
				position.y = (float)ten_lua[i].y;
				        sprite_handler->Draw(
			p_ten_lua, 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));

			}
		}
	
	//	ve(menuItemSelected);
 //       //stop drawing
        sprite_handler->End();
 //      
 //       //stop rendering
        d3ddev->EndScene();
    }
}

void Sences::chay()
{
	switch (this->man) {
	case 1:
		this->man1();
		break;
	default:
		this->menu();
	}
}




